<template>
    <div>
      <div
      style="
        margin-top: 15px;
        width: 300px;
        margin-left: 45px;
        margin-bottom: 15px;
      "
    >
      <el-input
        placeholder="请输入用户名"
        v-model="query"
        class="input-with-select"
        :clearable="true"
        @blur="reList()"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getUserByUsername()"
        ></el-button>
      </el-input>
    </div>
        <!-- 表格部分 -->
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="user_id"
        label="用户编号"
        width="100%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="username"
        label="用户名" 
        align="center"
      >
    </el-table-column>
      <el-table-column
        fixed
        prop="phone"
        label="联系电话" 
        align="center"
      >
    </el-table-column>
      
      <el-table-column
        fixed
        prop="address"
        label="联系地址"
        align="center"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button @click="editClick(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
          <el-button v-show="scope.row.is_employed === 1"  @click="deletClick(scope.row)" type="danger" size="small"
            >离职</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-sizes="[5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    ></el-pagination>
    <el-drawer
      title="用户信息详情"
      :visible.sync="drawer"
      :before-close="handleClose"
    >
      <div>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <div>{{ handle_obj.service_name }}</div>
          </div>
          <div class="text item">
            {{ "用户id：" + handle_obj.user_id }}
          </div>
          <div class="text item">{{ "用户名：" + handle_obj.username }}</div>
          <div class="text item">{{ "联系电话" + handle_obj.phone }}</div>
          <el-form :model="handle_obj" label-width="100px">
            <el-form-item label="密码" required>
              <el-input
              type="textarea" :rows="3"
                v-model="handle_obj.password"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item label="联系地址" required>
              <el-input
                v-model="handle_obj.address"
                placeholder="请输入联系地址"
              ></el-input>
            </el-form-item>
            <el-button type="success" style="margin:auto 50% ;" @click="update()">提交修改</el-button>
          </el-form>
        </el-card>
      </div>
    </el-drawer>
    </div>
</template>

<script>
import {getUserList,getUserInfoByUsername,updateUserInfo} from "@/utils/request";
export default {
    name: 'DyymanagevueUserManagermentPage',

    data() {
        return {
          query:'',
          drawer: false,
          handle_obj: "", //操作对象
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 5, // 每页显示条数
            totalItems: 0, // 总记录数
        };
    },

    mounted() {
        this.getList();
    },

    methods: {
        async getList() {
      await getUserList()
        .then((res) => {
          this.tableData = res.data.data;
        })
        .catch((error) => {
          // 处理错误
          console.error(error);
        });
    },
    reList() {
     
        this.getList();
    
    },
    getUserByUsername(){
      if(this.query!=''){
        getUserInfoByUsername(this.query).then((res)=>{
          this.tableData = res.data.data;
        })
      }
    },
    editClick(operator) {
      this.drawer = true;
      this.handle_obj = operator;
    },
    handleClose(done) {
      //   this.$confirm("确认关闭？")
      //     .then((_) => {

      //     })
      //     .catch((_) => {});
      done();
      this.handle_obj = "";
    },
    update(){
      updateUserInfo(this.handle_obj).then((res)=>{
        if(res.status){
          this.$message.success("更新成功")
        }
        this.reList()
      })
    },
    // 分页器页码变化事件处理函数
    handleCurrentChange(newPage) {
      this.currentPage = newPage; // 更新当前页码
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize; //更新每页数据条数
    },
    
    },
};
</script>

<style scoped>
.el-pagination {
  position: absolute;
  bottom: 0;
  left: 45%;
  /* width: 30%; */
  bottom: 5%;
  background-color: #fff;
  z-index: 999; /* 可以根据需要设置z-index值，确保分页器显示在其他内容上方 */
  box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.1); /* 添加阴影效果（可选） */
}
</style>